﻿Imports Data


Public Class Proveedores

    Property id_empleado As Integer

    Private Sub IngresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles IngresarToolStripMenuItem.Click
        Me.LimpiarInterfaz()
    End Sub

    Private Function LimpiarInterfaz() As Boolean
        Try
            Me.txtDireccion.Text = String.Empty
            Me.txtNombre.Text = String.Empty
            Me.txtComuna.Text = String.Empty
            Me.txtEmail.Text = String.Empty
            Me.txtRut.Text = String.Empty
            Me.txtNombreContacto.Text = String.Empty
            Me.txtTelfCasa.Text = String.Empty
            Me.txtTelfCelular.Text = String.Empty
            Me.txtRut.Focus()
            Me.btnIngresar.Text = "Ingresar"
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub MdificarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles MdificarToolStripMenuItem.Click
        Try
            Me.ModificarInterfaz()
        Catch ex As Exception

        End Try
    End Sub

    Function ModificarInterfaz() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Me.id_empleado = Me.lvProveedor.SelectedItems(0).Name
                Dim proveedor = db.Proveedores.Where(Function(p) p.id = id_empleado).Select(Function(p) p).FirstOrDefault

                Me.txtNombre.Text = proveedor.nombre
                Me.txtComuna.Text = proveedor.comuna
                Me.txtDireccion.Text = proveedor.direccion
                Me.txtEmail.Text = proveedor.email
                Me.txtNombreContacto.Text = proveedor.nombre_contacto
                Me.txtTelfCasa.Text = proveedor.telefono
                Me.txtTelfCelular.Text = proveedor.telefono_oficina
                Me.txtRut.Text = proveedor.rut

                Me.btnIngresar.Text = "Modificar"

            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub Proveedores_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            If (Me.CargaLV) Then
                MsgBox("Carga Exitoso")
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function CargaLV() As Boolean
        Try
            Me.lvProveedor.Items.Clear()
            Using db As New SerigraphicaDBDataContext
                Dim l_proveedores As List(Of Data.Proveedores) = db.Proveedores.Select(Function(p) p).ToList
                For Each item In l_proveedores
                    Me.lvProveedor.Items.Add(New ListViewItem(New String() {item.nombre,
                                                                            item.direccion,
                                                                            item.email,
                                                                            item.nombre_contacto,
                                                                            item.telefono,
                                                                            item.telefono_oficina
                                                                            }) With {.Name = item.id})
                Next
            End Using

            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnIngresar_Click(sender As System.Object, e As System.EventArgs) Handles btnIngresar.Click
        Try
            If Me.btnIngresar.Text = "Ingresar" Then
                Dim proveedor As New Data.Proveedores With {.nombre = txtNombre.Text, _
                                                            .rut = txtRut.Text, _
                                                            .direccion = txtDireccion.Text, _
                                                            .nombre_contacto = txtNombreContacto.Text, _
                                                            .comuna = txtComuna.Text, _
                                                            .telefono = txtTelfCasa.Text, _
                                                            .telefono_oficina = txtTelfCelular.Text, _
                                                            .email = txtEmail.Text}

                If Me.IngresarProveedor(proveedor) Then
                    MsgBox("Ingresado Correctamente", MsgBoxStyle.OkCancel)
                    Me.LimpiarInterfaz()
                    Me.CargaLV()
                End If
            Else
                Me.id_empleado = Me.lvProveedor.SelectedItems(0).Name
                If Me.ModificarProveedor(id_empleado) Then
                    MsgBox("Modificado Correctamente", MsgBoxStyle.OkCancel)
                    Me.LimpiarInterfaz()
                    Me.CargaLV()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function IngresarProveedor(ByVal proveedor As Data.Proveedores) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                db.Proveedores.InsertOnSubmit(proveedor)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function


    Private Function ModificarProveedor(ByVal id As Integer) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim proveedor As Data.Proveedores = db.Proveedores.Where(Function(p) p.id = id).Select(Function(p) p).FirstOrDefault
                proveedor.comuna = txtComuna.Text
                proveedor.direccion = txtDireccion.Text
                proveedor.email = txtEmail.Text
                proveedor.nombre = txtNombre.Text
                proveedor.nombre_contacto = txtNombreContacto.Text
                proveedor.rut = txtRut.Text
                proveedor.telefono = txtTelfCasa.Text
                proveedor.telefono_oficina = txtTelfCelular.Text
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub EliminarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Try
            id_empleado = Me.lvProveedor.SelectedItems(0).Name
            If (Me.EliminarProveedor(id_empleado)) Then
                MsgBox("Eliminacion Exitosa", MsgBoxStyle.OkCancel)
                Me.LimpiarInterfaz()
                Me.CargaLV()
            Else
                MsgBox("Error", MsgBoxStyle.OkCancel)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function EliminarProveedor(ByVal id As Integer) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim proveedor As Data.Proveedores = db.Proveedores.Where(Function(p) p.id = id).Select(Function(p) p).FirstOrDefault
                db.Proveedores.DeleteOnSubmit(proveedor)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Try
            Me.Close()
        Catch ex As Exception

        End Try
    End Sub
End Class